home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mg2a_src.zip / WORD.C < prev   
C/C++ Source or Header  |  1988-08-23  |  6KB  |  253 lines

  1. /*
  2.  *        Word mode commands.
  3.  * The routines in this file
  4.  * implement commands that work word at
  5.  * a time. There are all sorts of word mode
  6.  * commands.
  7.  */
  8. #include    "def.h"
  9.  
  10. /*
  11.  * Move the cursor backward by
  12.  * "n" words. All of the details of motion
  13.  * are performed by the "backchar" and "forwchar"
  14.  * routines.
  15.  */
  16. /*ARGSUSED*/
  17. backword(f, n)
  18. {
  19.     if (n < 0) return forwword(f | FFRAND, -n);
  20.     if (backchar(FFRAND, 1) == FALSE)
  21.         return FALSE;
  22.     while (n--) {
  23.         while (inword() == FALSE) {
  24.             if (backchar(FFRAND, 1) == FALSE)
  25.                 return TRUE;
  26.         }
  27.         while (inword() != FALSE) {
  28.             if (backchar(FFRAND, 1) == FALSE)
  29.                 return TRUE;
  30.         }
  31.     }
  32.     return forwchar(FFRAND, 1);
  33. }
  34.  
  35. /*
  36.  * Move the cursor forward by
  37.  * the specified number of words. All of the
  38.  * motion is done by "forwchar".
  39.  */
  40. /*ARGSUSED*/
  41. forwword(f, n)
  42. {
  43.     if (n < 0)
  44.         return backword(f | FFRAND, -n);
  45.     while (n--) {
  46.         while (inword() == FALSE) {
  47.             if (forwchar(FFRAND, 1) == FALSE)
  48.                 return TRUE;
  49.         }
  50.         while (inword() != FALSE) {
  51.             if (forwchar(FFRAND, 1) == FALSE)
  52.                 return TRUE;
  53.         }
  54.     }
  55.     return TRUE;
  56. }
  57.  
  58. /*
  59.  * Move the cursor forward by
  60.  * the specified number of words. As you move,
  61.  * convert any characters to upper case.
  62.  */
  63. /*ARGSUSED*/
  64. upperword(f, n)
  65. {
  66.     register int    c;
  67.  
  68.     if (n < 0) return FALSE;
  69.     while (n--) {
  70.         while (inword() == FALSE) {
  71.             if (forwchar(FFRAND, 1) == FALSE)
  72.                 return TRUE;
  73.         }
  74.         while (inword() != FALSE) {
  75.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  76.             if (ISLOWER(c) != FALSE) {
  77.                 c = TOUPPER(c);
  78.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  79.                 lchange(WFHARD);
  80.             }
  81.             if (forwchar(FFRAND, 1) == FALSE)
  82.                 return TRUE;
  83.         }
  84.     }
  85.     return TRUE;
  86. }
  87.  
  88. /*
  89.  * Move the cursor forward by
  90.  * the specified number of words. As you move
  91.  * convert characters to lower case.
  92.  */
  93. /*ARGSUSED*/
  94. lowerword(f, n)
  95. {
  96.     register int    c;
  97.  
  98.     if (n < 0) return FALSE;
  99.     while (n--) {
  100.         while (inword() == FALSE) {
  101.             if (forwchar(FFRAND, 1) == FALSE)
  102.                 return TRUE;
  103.         }
  104.         while (inword() != FALSE) {
  105.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  106.             if (ISUPPER(c) != FALSE) {
  107.                 c = TOLOWER(c);
  108.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  109.                 lchange(WFHARD);
  110.             }
  111.             if (forwchar(FFRAND, 1) == FALSE)
  112.                 return TRUE;
  113.         }
  114.     }
  115.     return TRUE;
  116. }
  117.  
  118. /*
  119.  * Move the cursor forward by
  120.  * the specified number of words. As you move
  121.  * convert the first character of the word to upper
  122.  * case, and subsequent characters to lower case. Error
  123.  * if you try and move past the end of the buffer.
  124.  */
  125. /*ARGSUSED*/
  126. capword(f, n)
  127. {
  128.     register int    c;
  129.     VOID        lchange();
  130.  
  131.     if (n < 0) return FALSE;
  132.     while (n--) {
  133.         while (inword() == FALSE) {
  134.             if (forwchar(FFRAND, 1) == FALSE)
  135.                 return TRUE;
  136.         }
  137.         if (inword() != FALSE) {
  138.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  139.             if (ISLOWER(c) != FALSE) {
  140.                 c = TOUPPER(c);
  141.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  142.                 lchange(WFHARD);
  143.             }
  144.             if (forwchar(FFRAND, 1) == FALSE)
  145.                 return TRUE;
  146.             while (inword() != FALSE) {
  147.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  148.                 if (ISUPPER(c) != FALSE) {
  149.                     c = TOLOWER(c);
  150.                     lputc(curwp->w_dotp, curwp->w_doto, c);
  151.                     lchange(WFHARD);
  152.                 }
  153.                 if (forwchar(FFRAND, 1) == FALSE)
  154.                     return TRUE;
  155.             }
  156.         }
  157.     }
  158.     return TRUE;
  159. }
  160.  
  161. /*
  162.  * Kill forward by "n" words.
  163.  */
  164. /*ARGSUSED*/
  165. delfword(f, n)
  166. {
  167.     register RSIZE    size;
  168.     register LINE    *dotp;
  169.     register int    doto;
  170.  
  171.     if (n < 0)
  172.         return FALSE;
  173.     if ((lastflag&CFKILL) == 0)        /* Purge kill buffer.    */
  174.         kdelete();
  175.     thisflag |= CFKILL;
  176.     dotp = curwp->w_dotp;
  177.     doto = curwp->w_doto;
  178.     size = 0;
  179.     while (n--) {
  180.         while (inword() == FALSE) {
  181.             if (forwchar(FFRAND, 1) == FALSE)
  182.                 goto out;    /* Hit end of buffer.    */
  183.             ++size;
  184.         }
  185.         while (inword() != FALSE) {
  186.             if (forwchar(FFRAND, 1) == FALSE)
  187.                 goto out;    /* Hit end of buffer.    */
  188.             ++size;
  189.         }
  190.     }
  191. out:
  192.     curwp->w_dotp = dotp;
  193.     curwp->w_doto = doto;
  194.     return (ldelete(size, KFORW));
  195. }
  196.  
  197. /*
  198.  * Kill backwards by "n" words. The rules
  199.  * for success and failure are now different, to prevent
  200.  * strange behavior at the start of the buffer. The command
  201.  * only fails if something goes wrong with the actual delete
  202.  * of the characters. It is successful even if no characters
  203.  * are deleted, or if you say delete 5 words, and there are
  204.  * only 4 words left. I considered making the first call
  205.  * to "backchar" special, but decided that that would just
  206.  * be wierd. Normally this is bound to "M-Rubout" and
  207.  * to "M-Backspace".
  208.  */
  209. /*ARGSUSED*/
  210. delbword(f, n)
  211. {
  212.     register RSIZE    size;
  213.     VOID        kdelete();
  214.  
  215.     if (n < 0) return FALSE;
  216.     if ((lastflag&CFKILL) == 0)        /* Purge kill buffer.    */
  217.         kdelete();
  218.     thisflag |= CFKILL;
  219.     if (backchar(FFRAND, 1) == FALSE)
  220.         return (TRUE);            /* Hit buffer start.    */
  221.     size = 1;                /* One deleted.        */
  222.     while (n--) {
  223.         while (inword() == FALSE) {
  224.             if (backchar(FFRAND, 1) == FALSE)
  225.                 goto out;    /* Hit buffer start.    */
  226.             ++size;
  227.         }
  228.         while (inword() != FALSE) {
  229.             if (backchar(FFRAND, 1) == FALSE)
  230.                 goto out;    /* Hit buffer start.    */
  231.             ++size;
  232.         }
  233.     }
  234.     if (forwchar(FFRAND, 1) == FALSE)
  235.         return FALSE;
  236.     --size;                    /* Undo assumed delete. */
  237. out:
  238.     return ldelete(size, KBACK);
  239. }
  240.  
  241. /*
  242.  * Return TRUE if the character at dot
  243.  * is a character that is considered to be
  244.  * part of a word. The word character list is hard
  245.  * coded. Should be setable.
  246.  */
  247. inword() {
  248. /* can't use lgetc in ISWORD due to bug in OSK cpp */
  249.     return curwp->w_doto != llength(curwp->w_dotp) && 
  250.         ISWORD(curwp->w_dotp->l_text[curwp->w_doto]);
  251. }
  252.  
  253.